<template>
<!-- 综合查询/订单详情 -->
  <div style="padding: 15px">
    <vxe-toolbar
      id="inventoryReport"
      :custom="{ storage: true }"
      export
      :refresh="{ query: findList }"
    >
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
        <el-button @click="AllDataEventAll">导出全部</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      keep-source
      highlight-current-row
      :export-config="{ type: 'csv' }"
      :align="allAlign"
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"
      :loading="loading"
      :data="tableData"
      :edit-config="{ trigger: 'click', mode: 'row', showStatus: true }"
      sort-config.remote
      @sort-change="sortList"
      :seq-config="{
        startIndex: listQuery.pageNum * listQuery.pageSize - listQuery.pageSize,
      }"
    >
      <vxe-table-column type="seq" width="60"></vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="code"
          title="出库单号"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.code"
            placeholder="出库单号"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="isvCode"
          title="ISV单号"
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.isvCode"
            placeholder="ISV单号"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="status"
          title="单据状态"
          min-width="200"
          sortable
          :formatter="formatterStatus"
        />
        <template v-slot:header="{ column }">
          <el-select
            v-model="listQuery.status"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in statusList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="type"
          title="单据类型"
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-select
            v-model="listQuery.type"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in typeList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="shipmentName"
          title="货主"
          min-width="200"
          sortable
          show-header-overflow
          show-overflow
        />
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.shipmentCode" placeholder="货主" clearable>
            <el-option
              v-for="item in shipmentsData"
              :key="item.partyCode"
              :label="item.userName"
              :value="item.partyCode"
            >
            </el-option>
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="shopNick"
          title="店铺"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.shopNick" placeholder="店铺" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="sourceCode"
          title="来源单号"
          min-width="200"
          sortable
          show-header-overflow
          show-overflow
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.sourceCode"
            placeholder="来源单号"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="sku"
          title="订单品数"
          min-width="180"
        />
        <template v-slot:header="{ column }">
          <el-row type="flex" align="middle">
            <el-input
              v-model="listQuery.skuFloor"
              placeholder="下限"
              type="number"
              style="width: 45%"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.skuCeiling"
              placeholder="上限"
              type="number"
              style="width: 45%"
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="totalNum"
          title="订单件数"
          min-width="180"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-row type="flex" align="middle">
            <el-input
              v-model="listQuery.totalNumFloor"
              placeholder="下限"
              type="number"
              style="width: 45%"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.totalNumCeiling"
              placeholder="上限"
              type="number"
              style="width: 45%"
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="receiverName"
          title="收货人"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.receiverName"
            placeholder="收货人"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="receiverProvince"
          title="省"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.receiverProvince"
            placeholder="省"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="receiverCity"
          title="市"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.receiverCity"
            placeholder="市"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="receiverDistrict"
          title="区"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.receiverDistrict"
            placeholder="区"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="receiverDetailAddress"
          title="收货地址"
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.receiverDetailAddress"
            placeholder="收货地址"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="carrierName"
          title="承运商"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.carrierCode" clearable filterable placeholder="承运商">
            <el-option
              v-for="item in carrierList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          title="接收时间"
          min-width="320"
          sortable
          show-header-overflow
          show-overflow
        />
        <template v-slot:header="{ column }">
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width: 100%"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="(value) => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="outboundCompletionTime"
          title="出库完成时间"
          min-width="320"
          sortable
          show-header-overflow
          show-overflow
        />
        <template v-slot:header="{ column }">
          <el-date-picker
            v-model="outboundCompletionTimeRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width: 100%"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="(value) => changeDate(value, 'outboundCompletionTime')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="packageNum" title="包裹数" min-width="300" sortable />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.packageNumStart"
            placeholder="下限"
            clearable
            style="width: 45%"
            type="number"
          />
          -
          <el-input
            v-model="listQuery.packageNumEnd"
            placeholder="上限"
            clearable
            style="width: 45%"
            type="number"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="waybillCode"
          title="运单号"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.waybillCode"
            placeholder="运单号"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="wavePickingCode"
          title="波次号"
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.wavePickingCode"
            placeholder="波次号"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="orderNum"
          title="波次顺序号"
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            type="number"
            onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode || event.which))) || event.which === 8"
            v-model="listQuery.orderNum"
            placeholder="波次顺序号"
            clearable
          />
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import { getWarehouseSpace } from "@/api/basicInfo";
import { importDeliveryList } from "@/api/comprehensive";
import { shipments } from "@/api/packingMaterial/packingMaterial";
import { carrierDropdown } from '@/api/outStorage'
import { exportTask } from '@/api/basicInfo'
export default {
  name: "OrderDetails",
  components: {},
  data() {
    return {
      typeList: [
        { code: "0", name: "销售出库" },
        { code: "1", name: "报损出库" },
        { code: "2", name: "领用出库" },
        { code: "3", name: "盈亏出库" },
      ],
      statusList: [
        { code: '0', name: '新增' },
        { code: '1', name: '待分配' },
        { code: '2', name: '部分分配' },
        { code: '3', name: '已分配' },
        { code: '6', name: '待拣货' },
        { code: '7', name: '待复核' },
        { code: '8', name: '复核中' },
        { code: '9', name: '待出库' },
        { code: '11', name: '出库完成' },
        { code: '12', name: '拣货中' },
        { code: '13', name: '拣货完成' }
      ],
      isDefaultOptions: [
        { label: '否', value: 0 },
        { label: '是', value: 1 }
      ],
      shipmentsData: [],
      carrierList: [],
      gmtCreatedRange: '',
      outboundCompletionTimeRange: '',
      // 日期选项
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      loading: false,
      allAlign: "center",
      tableData: [],
      gmtManufactureRange: "",
      gmtExpireRange: "",
      gmtStockRange: "",
      spaceData: [], //库区列表
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
    };
  },
  created() {
    this.findList();
    shipments().then((res) => {
      this.shipmentsData.push(...res.data);
    });
    carrierDropdown().then(res => {
      this.carrierList = res.data
    })
  },
  methods: {
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    },
    //  获取列表
    findList() {
      this.loading = true;
      return new Promise((reslove) => {
        importDeliveryList(this.listQuery).then((res) => {
          this.tableData = res.data.records
          this.listQuery.total = res.data.total
          this.loading = false;
          reslove();
        });
        // 库区下拉
        getWarehouseSpace(this.listQuery.warehouseCode).then((res) => {
          this.spaceData = res.data;
        });
      });
    },
    formatterStatus({ cellValue }) {
      const item = this.statusList.find(
        item => item.code === cellValue
      )
      return item ? item.name : ''
    },
    // 翻译是否默认
    formatterIsDefault({ cellValue }) {
      const item = this.isDefaultOptions.find(
        item => item.value === cellValue
      )
      return item ? item.label : ''
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
    // 导出全部
    AllDataEventAll () {
      let params = {
        dataType: 'DELIVERED',
        filter: this.listQuery
      }
      exportTask(params).then(data => {
        this.$message.success(data.msg)
      })
    }
  },
};
</script>
<style>
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    appearance: none;
    margin: 0;
}

</style>
